package com.zzti.controller;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Connection;
import com.zzti.utils.DBCutils;

/**
 * Servlet implementation class InsterServlet
 */
@WebServlet("/InsterServlet")
public class InsterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InsterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		String name=request.getParameter("name");
		String sex=request.getParameter("sex");
		int age=Integer.parseInt(request.getParameter("age"));
		String dapartment=request.getParameter("dapartment");
		String type=request.getParameter("type1");
		int year=Integer.parseInt(request.getParameter("year"));
		Connection connection = DBCutils.getConnection();
		String sql = "insert into employee(name,sex,age,dapartment,type,year)"+"values(?,?,?,?,?,?)";
		PreparedStatement psmt;
		try {
			psmt = connection.prepareStatement(sql);
			psmt.setString(1, name);
			psmt.setString(2, sex);
			psmt.setInt(3, age);
			psmt.setString(4, dapartment);
			psmt.setString(5, type);
			psmt.setInt(6, year);
			psmt.execute();
			connection.close();
			psmt.close();
			request.getRequestDispatcher("ItemServlet").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
